package main

import "fmt"

func main() {
	ch := make(chan int)

	go func() {
		for i := 0; i < 5; i++ {
			ch <- i
		}
		close(ch)
	}()

	for {
		data, ok := <-ch
		if ok {
			fmt.Println(data)
		} else {
			break
		}
	}

	//简便写法，关闭后停止循环
	for num := range ch {
		fmt.Println(num)
	}

}
